<template>
  <div>
    <el-form ref="vForm" label-position="left" label-width="80px" size="medium" @submit.native.prevent>
      <div>
        <div class="record-title">阿森斯失眠量表（AIS）</div>
      </div>
      <div class="static-content-item-head center">
        <div class="static-content-item">
          <div>填表人：</div>
          <!-- <div>{{user.fillPerson}}</div> -->
          <div><input :disabled="disabled" type="text" v-model="user.fillPerson" /></div>
        </div>
        <div class="static-content-item">
          <div>填表日期：</div>
          <el-date-picker :disabled="disabled" v-model="user.fillDate" value-format="yyyy-MM-ddTHH:mm:ss" type="datetime" placeholder="选择日期时间" />
          <!-- <div>{{user.fillDate}}</div> -->
          <!-- <div><input type="text" v-model="user.fillDate" /></div> -->
        </div>
        <div class="static-content-item">
          <div>第几次评定：</div>
          <!-- <div>{{user.evaluateNum}}</div> -->
          <div><input :disabled="disabled" type="text" v-model="user.evaluateNum" /></div>
        </div>
      </div>
      <div class="instructions center">
        <p style="text-indent: 2em;">本表主要用于记录您对遇到过的睡眠障碍的自我评估。对于以下列出的问题，如果在过去1个月内每星期至少发生3次在您身上，就请您在相应的“□”上打“√”：
        </p>
      </div>

      <div class="table-container">
        <table class="table-layout center">
          <tbody>
            <tr v-for="(item, index) in tableData" :key="index">
              <td class="table-cell">{{ item.title }}</td>
              <td class="table-cell">
                <el-radio-group :disabled="disabled" v-model="answers[index]">
                  <el-radio :label="0">{{ item.options[0].label }}</el-radio>
                </el-radio-group>
              </td>
              <td class="table-cell">
                <el-radio-group :disabled="disabled" v-model="answers[index]">
                  <el-radio :label="1">{{ item.options[1].label }}</el-radio>
                </el-radio-group>
              </td>
              <td class="table-cell">
                <el-radio-group :disabled="disabled" v-model="answers[index]">
                  <el-radio :label="2">{{ item.options[2].label }}</el-radio>
                </el-radio-group>
              </td>
              <td class="table-cell">
                <el-radio-group v-model="answers[index]">
                  <el-radio :disabled="disabled" :label="3">{{ item.options[3].label }}</el-radio>
                </el-radio-group>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      <div class="instructions center">
        <p style="text-indent: 2em;">量表共8个条目，每条从无到严重分为0、1、2、3 四级评分
        </p>
        <p style="text-indent: 2em;">（总分小于4：无睡眠障碍；如果总分在4~6：可疑失眠；如果总分在6 分以上：失眠）。
        </p>
      </div>
    </el-form>
  </div>
</template>
<script>
import moment from 'moment';
export default {
  name: '',
  props: {
    data: { type: Object, default: () => ({}) }
  },
  data() {
    return {
      tableData: [
        {
          title: '1. 入睡时间（关灯后到睡着的时间）',
          options: [
            { label: '没问题' },
            { label: '轻微延迟' },
            { label: '显著延迟' },
            { label: '延迟严重或没有睡觉' }
          ]
        },
        {
          title: '2. 夜间苏醒',
          options: [
            { label: '没问题' },
            { label: '轻微影响' },
            { label: '显著影响' },
            { label: '严重影响或没有睡觉' }
          ]
        },
        {
          title: '3. 比期望的时间早醒',
          options: [
            { label: '没问题' },
            { label: '轻微提早' },
            { label: '显著提早' },
            { label: '严重提早或没有睡觉' }
          ]
        },
        {
          title: '4. 总睡眠时间',
          options: [
            { label: '足够' },
            { label: '轻微不足' },
            { label: '显著不足' },
            { label: '严重不足或没有睡觉' }
          ]
        },
        {
          title: '5. 总睡眠质量（无论睡多长）',
          options: [
            { label: '满意' },
            { label: '轻微不满' },
            { label: '显著不满' },
            { label: '严重不满或没有睡觉' }
          ]
        },
        {
          title: '6. 白天情绪',
          options: [
            { label: '正常' },
            { label: '轻微低落' },
            { label: '显著低落' },
            { label: '严重低落' }
          ]
        },
        {
          title: '7. 白天身体功能（体力或精神：如记忆力、认知力和注意力等）',
          options: [
            { label: '足够' },
            { label: '轻微影响' },
            { label: '显著影响' },
            { label: '严重影响' }
          ]
        },
        {
          title: '8. 白天思睡',
          options: [
            { label: '无思睡' },
            { label: '轻微思睡' },
            { label: '显著思睡' },
            { label: '严重思睡' }
          ]
        }
      ],
      answers: [],
      user: {
        fillPerson: null,
        fillDate: null,
        evaluateNum: 0
      },
      form: {
        question1Score: null,
        question2Score: null,
        question3Score: null,
        question4Score: null,
        question5Score: null,
        question6Score: null,
        question7Score: null,
        question8Score: null,
      },
    }
  },
  computed: {
    // 判断用户当前性别
    getGender() {
      switch (this.user.gender) {
        case 0: return "女";
        case 1: return "男";
        default: return null;
      }
    },
    // 判断是否需要禁用
    disabled() {
      return !!this.$route.query.type
    }
  },
  created() {
    this.initData();
  },
  methods: {
    initData() {
      Object.keys(this.user).forEach(key => this.user[key] = this.data[key]);
      this.user.fillDate = moment(this.user.fillDate).format("YYYY-MM-DDTHH:mm:ss");

      Object.keys(this.form).forEach(key => this.form[key] = this.data[key]);
      let list = Object.values(this.form).map(val => val);
      this.answers = list;
    },
    submitAnswers() {
      this.answers.forEach((val, index) => this.form[`question${index + 1}Score`] = val)
      return { ...this.form, ...this.user };
      // console.log(this.answers, "this.answers");
      // const answerValues = [];
      // this.answers.forEach((answerIndex, index) => {
      //   if (answerIndex !== null) {
      //     answerValues.push(this.tableData[index].options[answerIndex].label);
      //   } else {
      //     answerValues.push(null);
      //   }
      // });
      // console.log('提交的答案数组：', answerValues);
      // 这里可以添加向后端发送数据的代码，例如使用 axios
      // axios.post('/api/submit', { answers: answerValues })
      //    .then(response => {
      //        console.log('提交成功：', response.data);
      //    })
      //    .catch(error => {
      //        console.error('提交失败：', error);
      //    });
    }
  },
}
</script>
<style lang="scss" scoped>
.center {
  width: 80%;
  margin: 0 auto;
}

.record-title {
  font-size: 27px;
  font-weight: bold;
  text-align: center;
  margin: 20px 0;
}

.el-input-number.full-width-input,
.el-cascader.full-width-input {
  width: 100% !important;
}

.clear-fix:before,
.clear-fix:after {
  display: table;
  content: "";
}

.clear-fix:after {
  clear: both;
}

.float-right {
  float: right;
}

div.table-container {
  table.table-layout {
    table-layout: fixed;
    border-collapse: collapse;
    text-align: center;

    td.table-cell {
      display: table-cell;
      height: 38px;
      border: 1px solid #333;
      font-size: 16px;
      overflow-wrap: break-word;
    }
  }
}

.static-content-item {
  min-height: 20px;
  display: flex;
  align-items: center;
  margin-right: 20px;
  font-size: 16px;

  ::v-deep .el-divider--horizontal {
    margin: 0;
  }
}

.static-content-item-head {
  min-height: 20px;
  font-size: 15px;
  display: flex;
  // justify-content: space-around;
  justify-content: space-between;

  ::v-deep .el-divider--horizontal {
    margin: 0;
  }
}

::v-deep .el-divider--horizontal {
  margin: 0;
}

.el-form-item--small.el-form-item {
  margin-bottom: 0px;
}

::v-deep .el-form-item__label {
  font-size: 16px;
}

.el-form-item {
  margin-bottom: 0px;
}

.footer {
  font-size: 20px;

  input {
    text-align: left;
    width: 40%;
  }
}

//宽度
.w-50 {
  width: 50px;
}

.w-100 {
  width: 100px;
}

.w-150 {
  width: 150px;
}

.w-200 {
  width: 200px;
}

.instructions {
  font-size: 18px;
  line-height: 1.6;
  padding: 20px;
  border-radius: 5px;
}

.instructions p {
  margin-bottom: 10px;
}

// 去除input样式
input {
  border: none;
  text-align: center;
  width: 100%;
}

input[type="text"]:focus {
  outline: none;
}

input::placeholder {
  color: #ccc;
  /* 设置 placeholder 颜色为灰色 */
}

.textareaStyle {
  width: 100%;
  height: 93%;
  margin-top: 10px;
}

textarea {
  border: none;
  text-align: center;
  width: 100%;
}

textarea[type="text"]:focus {
  outline: none;
}

textarea::placeholder {
  color: #ccc;
  /* 设置 placeholder 颜色为灰色 */
}
</style>
